package com.tonghu.bluetoothregistration;

import android.app.usage.UsageEvents;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.tonghu.bluetoothregistration.adapter.MainViewPagerAdapter;
import com.tonghu.bluetoothregistration.bean.ScanData;
import com.tonghu.bluetoothregistration.core.ScanDeviceIBeacon;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import de.greenrobot.event.EventBus;


@EActivity(R.layout.activity_main)
public class MainActivity extends ActionBarActivity implements BluetoothAdapter.LeScanCallback {
    @ViewById
    ViewPager viewPager;
    ScanDeviceIBeacon scanDeviceIBeacon;
    PagerAdapter pagerAdapter;

    @AfterViews
    void afterViews() {
        pagerAdapter = new MainViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        scanDeviceIBeacon = new ScanDeviceIBeacon();
        scanDeviceIBeacon.setLeScanCallback(this);
        scanDeviceIBeacon.start(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
        ScanData scanData = ScanData.fromScanData(bluetoothDevice, i, bytes);
        EventBus.getDefault().post(scanData);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        scanDeviceIBeacon.stop();
    }
}
